package com.ziyun.erp.web.modules.report.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ziyun.erp.common.utils.UserUtils;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.report.entity.ErpReportEntity;
import com.ziyun.erp.modules.report.service.ErpReportService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 报表管理表
 *
 * @author guizhao
 * @date 2018-09-19 18:17:14
 */
@RestController
@RequestMapping("report/erpreport")
public class ErpReportController {
    @Autowired
    private ErpReportService erpReportService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    /*@RequiresPermissions("report:erpreport:list")*/
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = erpReportService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{reId}")
    @RequiresPermissions("report:erpreport:info")
    public R info(@PathVariable("reId") Integer reId){
        ErpReportEntity erpReport = erpReportService.selectById(reId);

        return R.ok().put("erpReport", erpReport);
    }

    /**
     * 保存  zw
     */
    @RequestMapping("/save")
    @RequiresPermissions("report:erpreport:save")
    public R save(@RequestBody ErpReportEntity erpReport){
        erpReport.setCreateName(UserUtils.getCurentUser().getUsername());
        String url = ".cpt";
        String type = erpReport.getReportCategory();
        String name = erpReport.getReportName();
        switch (type){
            case "finance":
                erpReport.setReportPath(type+"/"+name+url);break;
            case "supp":
                erpReport.setReportPath(type+"/"+name+url);break;
            case "store":
                erpReport.setReportPath(type+"/"+name+url);break;
            case "business":
                erpReport.setReportPath(type+"/"+name+url);break;
        }
        erpReportService.insert(erpReport);
        return R.ok();
    }

    /**
     * 修改 zw
     */
    @RequestMapping("/update")
    @RequiresPermissions("report:erpreport:update")
    public R update(@RequestBody ErpReportEntity erpReport){
        ValidatorUtils.validateEntity(erpReport);
        String url = ".cpt";
        String type = erpReport.getReportCategory();
        String name = erpReport.getReportName();
        switch (type){
            case "finance":
                erpReport.setReportPath(type+"/"+name+url);break;
            case "supp":
                erpReport.setReportPath(type+"/"+name+url);break;
            case "store":
                erpReport.setReportPath(type+"/"+name+url);break;
            case "business":
                erpReport.setReportPath(type+"/"+name+url);break;
        }
        erpReportService.updateAllColumnById(erpReport);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("report:erpreport:delete")
    public R delete(@RequestBody Integer reId){
        erpReportService.deleteUpdate(reId);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/querylist")
    public R querylist(@RequestParam String type){
        return erpReportService.querylist(type);
    }

    /**
     * 报表名称
     */
    @RequestMapping("/getUreportName")
    public R getUreportName(@RequestBody Integer uId){
        return erpReportService.getUreportName(uId);
    }

    /**
     * ureport map
     */
    @RequestMapping("/ureportMaplist")
    public R ureportMaplist(){
        return erpReportService.ureportMaplist();
    }

    /**
     * ureport designer
     */
    @RequestMapping("/getUreportDesigner")
    public R getUreportDesigner(){
        return erpReportService.getUreportDesigner();
    }

}
